package io.xxx.omni.sync.impl

import com.alibaba.fastjson.JSON
import io.xxx.omni.sync.Document
import io.xxx.omni.sync.Porter
import io.xxx.omni.sync.Store
import io.xxx.omni.util.dateTimeFormatter
import org.springframework.stereotype.Component
import org.springframework.web.util.UriComponentsBuilder
import java.net.URI
import java.time.LocalDateTime

/**
 * POS
 */
@Component
class PosPorter : Porter() {

    private val httpUrl = "https://cloudpos.pureh2b.com/mpos/orders/sync"

    override val platformCode = "pos"

    override val jobId = 1  // POS接口不区分订单和退单

    override fun getUrl(store: Store, startTime: LocalDateTime, endTime: LocalDateTime, parameter: Any?): URI {
        return UriComponentsBuilder.fromHttpUrl(httpUrl)
            .queryParam("startTime", startTime.format(dateTimeFormatter))
            .queryParam("endTime", endTime.format(dateTimeFormatter))
            .queryParam("storeCode", store.code)
            .build(false)
            .toUri()
    }

    override fun buildDocuments(data: List<String>): List<Document> {
        return data.map {
            val value = JSON.parseObject(it)
            val modified = LocalDateTime.parse(value.getString("updatedTime"))
            Document(value.getString("id"), value.getString("originalOrderId"), value, modified)
        }
    }
}